/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import Controller.exceptions.NonexistentEntityException;
import Entities.Clientes;
import Entities.ClientesContactos;
import java.io.Serializable;
import java.util.List;
import java.util.Vector;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Victor
 */
public class ClientesContactosJpaController implements Serializable {

    public ClientesContactosJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(ClientesContactos clientesContactos) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(clientesContactos);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(ClientesContactos clientesContactos) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            clientesContactos = em.merge(clientesContactos);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = clientesContactos.getId();
                if (findClientesContactos(id) == null) {
                    throw new NonexistentEntityException("The clientesContactos with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ClientesContactos clientesContactos;
            try {
                clientesContactos = em.getReference(ClientesContactos.class, id);
                clientesContactos.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The clientesContactos with id " + id + " no longer exists.", enfe);
            }
            em.remove(clientesContactos);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void BorrarPoridCliente(Clientes idCliente){
        EntityManager em = getEntityManager();
        try{
          em.getTransaction().begin();
          List<ClientesContactos> Lista = (List<ClientesContactos>) em.createNamedQuery("ClientesContactos.findByidCliente")     
                    .setParameter("idCliente", idCliente.getId())
                    .getResultList();
            for (ClientesContactos item : Lista) {
                em.remove(item);
            }  
          em.getTransaction().commit();          
        } finally {
          em.close();          
        }
        
    }
    
    public List<ClientesContactos> findClientesContactosEntities() {
        return findClientesContactosEntities(true, -1, -1);
    }

    public List<ClientesContactos> findClientesContactosEntities(int maxResults, int firstResult) {
        return findClientesContactosEntities(false, maxResults, firstResult);
    }

    private List<ClientesContactos> findClientesContactosEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(ClientesContactos.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public ClientesContactos findClientesContactos(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ClientesContactos.class, id);
        } finally {
            em.close();
        }
    }

    public int getClientesContactosCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<ClientesContactos> rt = cq.from(ClientesContactos.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
    public List<ClientesContactos> ListarPoridCliente(int idCliente ) throws Exception{    
        List<ClientesContactos> Lista = null;
        EntityManager em = getEntityManager();
        Lista = (List<ClientesContactos>) em.createNamedQuery("ClientesContactos.findByidCliente")
                .setParameter("idCliente", idCliente)
                .getResultList();  
        
        return Lista;
    }  
    
    
    public DefaultTableModel CragarModeloListar (List<ClientesContactos> lista,DefaultTableModel modelotabla){
        int i =0;
        for(i=0;i<lista.size();i++){
            Vector<Object> vect = new Vector<>();
            vect.addElement(lista.get(i).getNombre());                     
            vect.addElement(lista.get(i).getCargo());
            vect.addElement(lista.get(i).getTelefonos());
            vect.addElement(lista.get(i).getEmail());
            modelotabla.addRow(vect);
        } 
        return modelotabla;
    }
    
    public void VaciarModeloListar(DefaultTableModel modelotabla){
        int c = modelotabla.getRowCount();
        int i =0;
        c--;
        if (c>=-1){
            for(i=c;i>=0;i--){
                modelotabla.removeRow(i);
            }
        }
    }
    
}
